package com.example.configuration;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4;
import com.example.filter.LoginHandlerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Autowired
    private LoginHandlerInterceptor loginHandlerInterceptor;

//    @Override
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginHandlerInterceptor)
                .excludePathPatterns("/UserLogin", "/login/password", "/loginByToken")
                .excludePathPatterns("/static/**")
                .addPathPatterns("/**");
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:favicon.ico");
    }

    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        return new HttpMessageConverters(createFastJsonConverter());
    }

    private static HttpMessageConverter createFastJsonConverter() {
        FastJsonHttpMessageConverter4 fastConverter = new FastJsonHttpMessageConverter4() {

            @Override
            protected boolean supports(Class<?> paramClass) {
                return paramClass != String.class;
            }

        };
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        fastConverter.setFastJsonConfig(fastJsonConfig);
        List<MediaType> supportedMediaTypes = new ArrayList<>();
        supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastConverter.setSupportedMediaTypes(supportedMediaTypes);

        //自定义格式化输出
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat,
                SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteDateUseDateFormat,
                SerializerFeature.DisableCircularReferenceDetect);//禁止循环引用
        FastJsonHttpMessageConverter4 fastjson = new FastJsonHttpMessageConverter4();
        fastjson.setFastJsonConfig(fastJsonConfig);

        return fastConverter;
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                //发布动研的路径
                .allowedOrigins("*")
                //调试的路径
//                .allowedOrigins("http://localhost:8090")
//                .allowCredentials(true)
                //动研方案
                .allowedMethods("*") // 允许的请求方法

                .allowedHeaders("Accept",
                        "Accept-Encoding",
                        "Accept-Language",
                        "Cache-Control",
                        "Connection",
                        "Content-Length",
                        "Content-Type",
                        "Host",
                        "Origin",
                        "Pragma",
                        "Referer",
                        "Sec-Ch-Ua",
                        "Sec-Ch-Ua-Mobile",
                        "Sec-Ch-Ua-Platform",
                        "Sec-Fetch-Dest",
                        "Sec-Fetch-Mode",
                        "Sec-Fetch-Site",
                        "User-Agent",
                        "Origin",
                        "Content-Type",
                        "Accept",
                        "Authorization",
                        "u_token")
                .maxAge(3600);
    }
}